using System;
using System.Threading;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
namespace com.tc.gateway.tunnel
{

    public class MultiCast
    {
        IPEndPoint group = null;
        UdpClient socket = null;
        Stream inputStream = null;
        Stream outputStream = null;
        int port;
        bool listen;
        public MultiCast(string addr, int p, bool join)
        {
            this.port = p;
            this.listen = join;
            IPAddress multicastIP = IPAddress.Parse(addr);
            IPEndPoint group = new IPEndPoint(multicastIP, p);

            socket = new UdpClient();
            if (listen)
                socket.JoinMulticastGroup(multicastIP, 10);
            socket.EnableBroadcast = true;


        }

        public Stream getInputStream()
        {
            if (!listen)
                throw new Exception("this socket has not join the group");
            if (inputStream == null)
                inputStream = new MultiInputStream(group, socket, port);
            return inputStream;
        }
        public Stream getOutputStream()
        {
            if (outputStream == null)
                outputStream = new MultiOutputStream(group, socket, port);
            return outputStream;
        }

    }
}